<template>
  <view class="goods-item "
        @click="toDetail(goods.goods_id)">
    <view class="goods-item-left">
      <slot name="radio"></slot>
      <!-- 自定义指令完成当没有图片的时候补充图片 -->
      <img :src="goods.goods_small_logo||imgURL"
           alt=""
           class="goods-pic">
    </view>
    <view class="goods-item-right">
      <text class="goods-name">￥{{goods.goods_name}}</text>
      <view class="price_number">
        <text class="goods-price">￥{{goods.goods_price | toFixed}}</text>
        <slot name="number" />
      </view>
    </view>
  </view>
</template>
<script>
export default ({
  // 接收传递过来的值
  props: {
    goods: {
      type: Object,
      default: () => ({})
    }
  },
  data () {
    return {
      imgURL: require('../../static/logo.png'),
    };
  },
  // 过滤器处理数据保存两位小数点
  // toFixed()可以处理整数后面保存几个小数点
  // 当123.3482.toFixed(2)结果为123.35
  filters: {
    toFixed (value) {
      return value.toFixed(2)
    }
  },
  methods: {
    // 点击商品跳转到商品详情页
    toDetail (id) {
      uni.navigateTo({
        url: '/subpckg/detail/index?id=' + id
      })
    }
  }
  // 自定义指令实现图片不在时自动填充
  // directives: {
  //   imgDefault: {
  //     inserted (dom, obj, vnode) {

  //       if (obj.src == '') {
  //         dom.src = obj.value
  //       }

  //     },
  //   }
  // }
})
</script>
<style lang="scss" scoped>
.goods-item {
  display: flex;
  padding: 10px 5px;
  border-bottom: 1px solid #f0f0f0;

  .goods-item-left {
    margin-right: 5px;
    display: flex;
    align-items: center;
    .goods-pic {
      width: 100px;
      height: 100px;
      display: block;
    }
  }

  .goods-item-right {
    display: flex;
    flex-direction: column;
    justify-content: space-between;

    .goods-name {
      font-size: 13px;
    }
    .price_number {
      display: flex;
      justify-content: space-around;
      align-items: center;
      .goods-price {
        font-size: 16px;
        color: #c00000;
      }
    }
  }
}
</style>
